package com.mobapping;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;

public class PharmacyItemizedOverlay  extends ItemizedOverlay {
	Context mContext;
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);	    
		populate();
	}

	public void clearOverlay(OverlayItem overlay) {
		mOverlays.clear();	    
	}

	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}

	public PharmacyItemizedOverlay(Drawable defaultMarker, Context context) {
		//super(defaultMarker);
		super(boundCenterBottom(defaultMarker));
		mContext = context;
	}

	@Override
	protected boolean onTap(int index) {
		final PharmacyOverlayItem item = (PharmacyOverlayItem)mOverlays.get(index);
		if(item.getName()!="Konumum"){
			final Dialog dialog = new Dialog(mContext);
			dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
			dialog.setContentView(R.layout.detailed_item_dialog);

			TextView nameView = (TextView)dialog.findViewById(R.id.name);
			nameView.setText(item.getName());
			TextView phoneView = (TextView)dialog.findViewById(R.id.phone);
			phoneView.setText("Tel: "+item.getPhone());
			TextView addressView = (TextView)dialog.findViewById(R.id.address);
			addressView.setText("Adres: "+item.getAddress());
			Button positiveButton = (Button) dialog.findViewById(R.id.positiveButton);
			Button negativeButton = (Button) dialog.findViewById(R.id.negativeButton);

			positiveButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					dialog.cancel();
				}
			});

			negativeButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					Intent callIntent = new Intent(Intent.ACTION_DIAL);
					callIntent.setData(Uri.parse("tel:"+item.getPhone()));
					mContext.startActivity(callIntent);
				}
			});


			dialog.show();
		}
		return true;
	}

	public void onClick(AlertDialog.Builder dialog) {
		//	dialog.dismiss(); 
	}


}
